# coding:utf-8

import requests
import json


class ShunFeng:
    def __init__(self, start_pro, start_city, des_pro, des_city):
        self.province_city_code = {
            u"广东深圳": "A440304000",
            u"浙江金华": "A330783000",
            u"青海西宁": "A630102000",
            u"西藏拉萨": "A540125000",
            u"上海上海": "A310105000",
            u"北京北京": "A110105000"
        }
        self.data = {
            "origin": self.province_city_code[start_pro + start_city],
            "dest": self.province_city_code[des_pro + des_city],
            "parentOrigin": self.province_city_code[start_pro + start_city],
            "parentDest": self.province_city_code[des_pro + des_city],
            "weight": "1",
            "time": "2017-07-21T12%3A00%3A00%2B08%3A00",
            "volume": "0",
            "queryType": "2",
            "lang": "sc",
            "region": "cn",
            "translate": ""
        }

    def get_url(self):
        url = 'http://www.sf-express.com/sf-service-owf-web/service/rate?'
        for key in self.data:
            url += '&' + key + '=' + str(self.data[key])
        return url

    def get_price(self):
        self.data["weight"] = 1
        r = requests.get(self.get_url())
        res_first = json.loads(r.text)
        first_one_day_price = first_two_day_price = second_one_day_price = second_two_day_price = 0
        for i in res_first:
            if i['limitTypeCode'] == 'T4':
                first_one_day_price = int(i['destFreight'])
            if i['limitTypeCode'] == 'T6':
                first_two_day_price = int(i['destFreight'])
        self.data["weight"] = 2
        r = requests.get(self.get_url())
        res_second = json.loads(r.text)
        for i in res_second:
            if i['limitTypeCode'] == 'T4':
                second_one_day_price = int(i['destFreight'])
            if i['limitTypeCode'] == 'T6':
                second_two_day_price = int(i['destFreight'])
        return [first_one_day_price, second_one_day_price - first_one_day_price], [first_two_day_price, second_two_day_price - first_two_day_price]
